﻿import random
from locust import HttpUser, task, between

from locust import events
headers = {'Content-Type': 'application/json;charset=utf-8'}

@events.test_start.add_listener
def on_test_start(**kwargs):
    print("A new test is starting")

@events.test_stop.add_listener
def on_test_stop(**kwargs):
    print("A new test is ending")

class Test1(HttpUser):
    weight = 1
    wait_time = between(0.001, 0.002)
    @task
    def productlist(self):
        item_id = random.randint(1, 10000)
        #查询产品列表
        data={"filter":{"prdType":"N","saleStatus":"1"},"paged":item_id}
        with self.client.post("/api/query/products",json=data, headers=headers, catch_response=True) as response:
            if response.status_code != 200:
                response.failure("返回状态码为【%s】，返回报文【%s】" % (response.status_code, response.text))
            else:
                json_resp = response.json()
                if json_resp["code"] != 1000:
                    response.failure("返回code为【%s】，返回报文【%s】" % (json_resp["code"], response.text))
                elif json_resp["msg"] != 'success':
                    response.failure("返回msg【%s】，返回报文【%s】" % (json_resp["msg"], response.text))

    def on_start(self):
        data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
        self.client.post("/api/user/general/login",json=data,
                         headers=headers)


class Test2(HttpUser):
    weight = 1
    wait_time = between(0.001, 0.002)
    #产品搜索
    @task
    def productSearch(self):
        with self.client.get("/api/query/product/search?keyword=橄榄油", headers=headers, catch_response=True) as response:
            if response.status_code != 200:
                response.failure("返回状态码为【%s】，返回报文【%s】" % (response.status_code, response.text))
            else:
                json_resp = response.json()
                if json_resp["code"] != 1000:
                    response.failure("返回code为【%s】，返回报文【%s】" % (json_resp["code"], response.text))

    def on_start(self):
        data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
        self.client.post("/api/user/general/login",json=data,
                         headers=headers)

class Test3(HttpUser):
    weight = 1
    wait_time = between(0.001, 0.002)
    #产品详情
    @task
    def productDetail(self):
        with self.client.get("/api/query/product/1000000361", headers=headers, catch_response=True) as response:
            if response.status_code != 200:
                response.failure("返回状态码为【%s】，返回报文【%s】" % (response.status_code, response.text))
            else:
                json_resp = response.json()
                if json_resp["code"] != 1000:
                    response.failure("返回code为【%s】，返回报文【%s】" % (json_resp["code"], response.text))

    def on_start(self):
        data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
        self.client.post("/api/user/general/login",json=data,
                         headers=headers)

class Test4(HttpUser):
    weight = 1
    wait_time = between(0.001, 0.002)
    #活动列表
    @task
    def activityList(self):
        with self.client.get("/api/activities?activityState=Y", headers=headers, catch_response=True) as response:
            if response.status_code != 200:
                response.failure("返回状态码为【%s】，返回报文【%s】" % (response.status_code, response.text))
            else:
                json_resp = response.json()
                if json_resp["code"] != 1000:
                    response.failure("返回code为【%s】，返回报文【%s】" % (json_resp["code"], response.text))

    def on_start(self):
        data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
        self.client.post("/api/user/general/login",json=data,
                         headers=headers)




# class TestN(HttpUser):
#     weight = 1
#     wait_time = between(5, 5)
#
#     @task
#     def fav_products(self):
#         data = {"prdItems": []}
#         with self.client.post("/api/delete/fav-products", headers=headers,json=data, catch_response=True) as response:
#             # json_resp = response.json()
#             # assert json_resp["code"] == 1000, "查询失败,返回状态码非1000"
#             if response.status_code != 200:
#                 response.failure("返回状态码为【%s】，返回报文【%s】" % (response.status_code, response.text))
#             else:
#                 json_resp = response.json()
#                 if json_resp["code"] != 1000:
#                     response.failure("返回code为【%s】，返回报文【%s】" % (json_resp["code"], response.text))
#
#     def on_start(self):
#         data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
#         self.client.post("/api/user/general/login",json=data,
#                          headers=headers)